package com.sec.android.app.myfiles.shadow.android;

import android.content.Context;
import android.widget.Toast;

import junit.framework.Assert;

import org.powermock.api.mockito.PowerMockito;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

/**
 * Created by jaebae on 2016-07-28.
 */
@Implements(Toast.class)
public class ShadowToast {
    static StringBuilder mMsgBuilder;
    public static boolean mEnableVerifyMsg = false;

    private static Toast mToast = PowerMockito.mock(Toast.class);

    public static void setEnableVerifyMsg(boolean enable) {
        mEnableVerifyMsg = enable;
        mMsgBuilder = null;
        if (mEnableVerifyMsg) {
            mMsgBuilder = new StringBuilder();
        }
    }

    public static void verifyMsg(String msg) {
        boolean bRet = false;

        if (mMsgBuilder != null) {
            int index = mMsgBuilder.indexOf(msg, 0);
            bRet = index >= 0;
        }

        Assert.assertTrue(msg, bRet);
    }

    @Implementation
    public static Toast makeText(Context context, int resId, int duration) {
        return makeText(context, context.getString(resId), duration);
    }

    @Implementation
    public static Toast makeText(Context context, CharSequence text, int duration) {
        if (mEnableVerifyMsg) {
            mMsgBuilder.append(text);
        }
        return mToast;
    }
}
